package test.iso
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import level4.iso.IsoUtil;

	public class DrawRectAreaIsoLine
	{
		public function DrawRectAreaIsoLine()
		{
		}
		
		public static function DrawRect(rect:Rectangle,tileWidth:int = 20):Sprite{
			var sprite:Sprite = new Sprite();
				sprite.graphics.clear();
				sprite.graphics.lineStyle(1,0xff0000);
			var hCount:int = rect.width/tileWidth;
			var vCount:int = rect.height/tileWidth*2;
			var tops:Array=[];
			var lefts:Array=[];
			var rights:Array=[];
			var bottoms:Array=[];
			var i:int=0;
			//top
			for(i=0;i<hCount;i++){
				tops.push(i,0);
			}
			//bottom
			for(i=0;i<hCount;i++){
				bottoms.push(i,vCount-1);
			}
			//left
			for(i=0;i<vCount;i++){
				lefts.push(0,i);
			}
			//right
			for(i=0;i<vCount;i++){
				rights.push(hCount-1,i);
			}
			
			var topPt:Point;
			var leftPt:Point;
			var bottomPt:Point;
			var rightPt:Point;
			var leftLastPt:Point;
			var topLastPt:Point;
			for(i=0;i<hCount;i++){
				var x:int = i * 2;
				var y:int = x + 1;
				var lastY:int = hCount*2 - 2*i -1;
				var lastX:int = lastY - 1;
				//top,left
				topPt = getPixelPoint(tops[x],tops[y]);
				leftPt = getPixelPoint(lefts[x],lefts[y]);
				//bottom,right
				bottomPt = getPixelPoint(bottoms[x],bottoms[y]);
				rightPt = getPixelPoint(rights[x],rights[y]);
				//lastLeft
				leftLastPt = getPixelPoint(lefts[lastX],lefts[lastY]);
				topLastPt = getPixelPoint(tops[lastX],tops[lastY]);
				//lastRight
				//左上方向
				lineTo(topPt,leftPt);
				lineTo(bottomPt,rightPt);
				//右下
				lineTo(bottomPt,leftLastPt);
				lineTo(topLastPt,rightPt);
			}
			return sprite;
			///////////////////////
			//闭包
			///////////////////////
			function lineTo(pt1:Point,pt2:Point):void{
				sprite.graphics.moveTo(pt1.x,pt1.y);
				sprite.graphics.lineTo(pt2.x,pt2.y);
			}
			function getPixelPoint(gridCoordX:int,gridCoordY:int):Point{
				return new Point(gridCoordX * tileWidth,gridCoordY * tileWidth / 2);
			}
		}
	}
}